package org.dark.fund.common;

import lombok.Builder;
import lombok.Data;
import org.dark.fund.enums.ResultCode;
import org.dark.fund.exceptions.FundException;

@Data
@Builder
public class ApiResult<T> {

    private String code;

    private String msg;

    private T data;

    public static <T> ApiResult<T> ok() {
        return (ApiResult<T>) ApiResult.builder().code(ResultCode.SUCCESS.getCode()).msg(ResultCode.SUCCESS.getMsg())
                .data(null).build();
    }

    public static <T> ApiResult<T> ok(T data) {
        return (ApiResult<T>) ApiResult.builder().code(ResultCode.SUCCESS.getCode()).msg(ResultCode.SUCCESS.getMsg())
                .data(data).build();
    }

    public static ApiResult<?> exception(Exception cause) {
        return ApiResult.builder().code(ResultCode.UNKNOWN_ERROR.getCode()).msg(cause.getMessage())
                .data(null).build();
    }

    public static ApiResult<?> exception(ResultCode resultCode, Exception cause) {
        return ApiResult.builder().code(resultCode.getCode()).msg(cause.getMessage())
                .data(null).build();
    }

    public static ApiResult<?> fundException(FundException bizException) {
        return ApiResult.builder().code(bizException.getMessage()).msg(bizException.getMsg())
                .data(null).build();
    }
}
